/* 
  封装axios实例的拦截器（请求、响应）
*/
import axios from "axios";
import Cookies from "js-cookie";

// 1.创建axios实例
const instance = axios.create({
  timeout: 15000, // 响应时间
  baseURL: "http://120.24.64.5:8088/mall-admin",
});

// 2.重写实例请求拦截器
instance.interceptors.request.use(
  config => {
    // 拿到token赋值到请求头中
    let token = Cookies.get("token");
    if (token && config.headers) {
      config.headers["Authorization"] = token;
    }

    return config;
  },
  err => {
    return Promise.reject(err);
  }
);

// 3.重写实例响应后拦截器(后置导航守卫)
instance.interceptors.response.use(
  result => {
    return result.data;
  },
  err => {
    return Promise.reject(err);
  }
);

export default instance;
